package org.ccay.upload.impl;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Named;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;

import org.ccay.core.ioc.Ccay;
import org.ccay.upload.IUploadFileConsumer;
import org.ccay.upload.IUploadSupportService;
import org.ccay.upload.UploadSettingVO;

@Named
public class UploadSupportService implements IUploadSupportService {
	public UploadSupportService() {
	}

	public UploadSettingVO findUploadSetting(String uploadType, UriInfo uriInfo) {
		return findUploadConsumer(uploadType).getUploadSetting(convert(uriInfo.getQueryParameters()));
	}

	private Map<String, String> convert(MultivaluedMap<String, String> queryParameters) {
		Map<String, String> map = new HashMap();
		for (String key : queryParameters.keySet()) {
			map.put(key, queryParameters.getFirst(key));
		}
		return map;
	}

	public IUploadFileConsumer findUploadConsumer(String uploadType) {
		return (IUploadFileConsumer) Ccay.getContext().getBean("IUploadFileConsumer." + uploadType, IUploadFileConsumer.class);
	}
}
